{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 性能和内存使用方面的注意事项\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "from datetime import timedelta\n",
    "from dateutil.parser import parse\n",
    "from pandas import DataFrame, Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000-01-01 00:00:00.000    1.379025\n",
       "2000-01-01 00:00:00.010   -0.287892\n",
       "2000-01-01 00:00:00.020   -0.084038\n",
       "2000-01-01 00:00:00.030   -0.859657\n",
       "2000-01-01 00:00:00.040    0.085484\n",
       "Freq: 10L, dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2000', periods=10000000, freq='10ms')\n",
    "ts = Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:00:00</th>\n",
       "      <td>1.379025</td>\n",
       "      <td>4.400398</td>\n",
       "      <td>-4.265419</td>\n",
       "      <td>0.669163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:15:00</th>\n",
       "      <td>0.857408</td>\n",
       "      <td>4.434345</td>\n",
       "      <td>-4.012007</td>\n",
       "      <td>-0.400203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:30:00</th>\n",
       "      <td>0.564459</td>\n",
       "      <td>4.703350</td>\n",
       "      <td>-4.218674</td>\n",
       "      <td>-0.321595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 00:45:00</th>\n",
       "      <td>0.981623</td>\n",
       "      <td>4.154236</td>\n",
       "      <td>-4.158338</td>\n",
       "      <td>0.606925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-01-01 01:00:00</th>\n",
       "      <td>-0.360400</td>\n",
       "      <td>4.217127</td>\n",
       "      <td>-4.148527</td>\n",
       "      <td>-0.005369</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         open      high       low     close\n",
       "2000-01-01 00:00:00  1.379025  4.400398 -4.265419  0.669163\n",
       "2000-01-01 00:15:00  0.857408  4.434345 -4.012007 -0.400203\n",
       "2000-01-01 00:30:00  0.564459  4.703350 -4.218674 -0.321595\n",
       "2000-01-01 00:45:00  0.981623  4.154236 -4.158338  0.606925\n",
       "2000-01-01 01:00:00 -0.360400  4.217127 -4.148527 -0.005369"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('15min').ohlc().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 144 ms per loop\n"
     ]
    }
   ],
   "source": [
    " %timeit ts.resample('15min').ohlc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 loop, best of 3: 224 ms per loop\n"
     ]
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2000', periods=10000000, freq='1s')\n",
    "ts = Series(np.random.randn(len(rng)), index=rng)\n",
    "%timeit ts.resample('15s').ohlc()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
